﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SHomeWorkshop.LunarPresentation.Widegets
{
    /// <summary>
    /// GridPanelAlignButtons.xaml 的交互逻辑
    /// </summary>
    public partial class GridPanelAlignButtons : UserControl
    {
        public GridPanelAlignButtons()
        {
            InitializeComponent();
        }

        private bool isCheckBoxsVisible = true;

        public bool IsCheckBoxsVisible
        {
            get { return isCheckBoxsVisible; }
            set
            {
                isCheckBoxsVisible = value;
                ckxAllColumnAlignment.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
                ckxAllLineAlignment.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        public event EventHandler<GridPanelAlignmentEventArgs> Clicked;

        protected void OnClicked(object sender, GridPanelAlignmentEventArgs e)
        {
            if (Clicked != null)
            {
                Clicked(sender, e);
            }
        }

        private void btnTopLeft_Click(object sender, RoutedEventArgs e)
        {
            OnClicked(this, new GridPanelAlignmentEventArgs(
                Expressions.GridPanel.GridPanelAlignment.TopLeft,
                GetGridPanelAlignmentTargetTypeValue()));
        }

        private GridPanelAlignmentTargetType GetGridPanelAlignmentTargetTypeValue()
        {
            GridPanelAlignmentTargetType slc;
            if (isCheckBoxsVisible)
            {
                if (ckxAllColumnAlignment.IsChecked == true)
                {
                    if (ckxAllLineAlignment.IsChecked == true)
                    {
                        slc = GridPanelAlignmentTargetType.SameLineAndColumnPanels;
                    }
                    else
                    {
                        slc = GridPanelAlignmentTargetType.SameColumnPanels;
                    }
                }
                else
                {
                    if (ckxAllLineAlignment.IsChecked == true)
                    {
                        slc = GridPanelAlignmentTargetType.SameLinePanels;
                    }
                    else
                    {
                        slc = GridPanelAlignmentTargetType.OnlySelfPanel;
                    }
                }
            }
            else
            {
                slc = GridPanelAlignmentTargetType.Ignore;
            }
            return slc;
        }

        private void btnTopCenter_Click(object sender, RoutedEventArgs e)
        {
            OnClicked(this, new GridPanelAlignmentEventArgs(
                Expressions.GridPanel.GridPanelAlignment.TopCenter,
                GetGridPanelAlignmentTargetTypeValue()));
        }

        private void btnTopRight_Click(object sender, RoutedEventArgs e)
        {
            OnClicked(this, new GridPanelAlignmentEventArgs(
                Expressions.GridPanel.GridPanelAlignment.TopRight,
                GetGridPanelAlignmentTargetTypeValue()));
        }

        private void btnLeft_Click(object sender, RoutedEventArgs e)
        {
            OnClicked(this, new GridPanelAlignmentEventArgs(
                Expressions.GridPanel.GridPanelAlignment.Left,
                GetGridPanelAlignmentTargetTypeValue()));
        }

        private void btnCenter_Click(object sender, RoutedEventArgs e)
        {
            OnClicked(this, new GridPanelAlignmentEventArgs(
                Expressions.GridPanel.GridPanelAlignment.Center,
                GetGridPanelAlignmentTargetTypeValue()));
        }

        private void btnRight_Click(object sender, RoutedEventArgs e)
        {
            OnClicked(this, new GridPanelAlignmentEventArgs(
                Expressions.GridPanel.GridPanelAlignment.Right,
                GetGridPanelAlignmentTargetTypeValue()));
        }

        private void btnBottomLeft_Click(object sender, RoutedEventArgs e)
        {
            OnClicked(this, new GridPanelAlignmentEventArgs(
                Expressions.GridPanel.GridPanelAlignment.BottomLeft,
                GetGridPanelAlignmentTargetTypeValue()));
        }

        private void btnBottomCenter_Click(object sender, RoutedEventArgs e)
        {
            OnClicked(this, new GridPanelAlignmentEventArgs(
                Expressions.GridPanel.GridPanelAlignment.BottomCenter,
                GetGridPanelAlignmentTargetTypeValue()));
        }

        private void btnBottomRight_Click(object sender, RoutedEventArgs e)
        {
            OnClicked(this, new GridPanelAlignmentEventArgs(
                Expressions.GridPanel.GridPanelAlignment.BottomRight,
                GetGridPanelAlignmentTargetTypeValue()));
        }

        private void UserControl_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (IsEnabled)
            {
                gridMiBorder.BorderBrush = Brushes.Red;
            }
            else
            {
                gridMiBorder.BorderBrush = Brushes.Gray;
            }
        }
    }

    /// <summary>
    /// 如果是只需要设置一个子面板的对齐方式，则用OnlySelfPanel（这是默认值）。
    /// 如果是对整个网格式中所有网格子面板都设置，使用Ignore。
    /// SameLineAndColumnPanels并不是所有行、所有列，而是与当前网格子面板在同行、同列的所有网格子面板——形如十字。
    /// </summary>
    public enum GridPanelAlignmentTargetType { OnlySelfPanel, SameLinePanels, SameColumnPanels, SameLineAndColumnPanels, Ignore }

    public class GridPanelAlignmentEventArgs : EventArgs
    {
        public GridPanelAlignmentEventArgs(Expressions.GridPanel.GridPanelAlignment align, GridPanelAlignmentTargetType alignmentTargetType)
        {
            this.newAlignValue = align;
            this.alignmentTargetType = alignmentTargetType;
        }

        private Expressions.GridPanel.GridPanelAlignment newAlignValue = Expressions.GridPanel.GridPanelAlignment.TopLeft;

        public Expressions.GridPanel.GridPanelAlignment NewAlignValue
        {
            get { return newAlignValue; }
            set { newAlignValue = value; }
        }

        private GridPanelAlignmentTargetType alignmentTargetType = GridPanelAlignmentTargetType.OnlySelfPanel;

        public GridPanelAlignmentTargetType AlignmentTargetType
        {
            get { return alignmentTargetType; }
            set { alignmentTargetType = value; }
        }
    }
}
